package com.example.peibei.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.dingtao.common.bean.BillRecordDetail;
import com.dingtao.common.bean.User;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.dingtao.common.util.UIUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.BillRecordDetailPresenter;
import com.example.peibei.service.presenter.GetOtherUserInfoPresenter;
import com.example.peibei.ui.utils.LevelImage;

import butterknife.BindView;
import butterknife.OnClick;

public class BillRecordDetailActivity extends WDActivity {
    @BindView(R.id.iv_user_head)
    ImageView iv_user_head;
    @BindView(R.id.tv_nickname)
    TextView tv_nickname;
    @BindView(R.id.tv_money)
    TextView tv_money;
    @BindView(R.id.tv_status)
    TextView tv_status;
    @BindView(R.id.tv_pay_cate)
    TextView tv_pay_cate;
    @BindView(R.id.tv_order_number)
    TextView tv_order_number;
    @BindView(R.id.tv_pay_type)
    TextView tv_pay_type;
    @BindView(R.id.tv_pay_time)
    TextView tv_pay_time;
    @BindView(R.id.tv_fee)
    TextView tv_fee;
    @BindView(R.id.tv_feeScale)
    TextView tv_feeScale;


    private SPUtils spUtils;
    private String token;
    private String mymid;
    private GetOtherUserInfoPresenter getOtherUserInfoPresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bill_record_detail;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        String logId = bundle.getString("logId");
        spUtils = new SPUtils(BillRecordDetailActivity.this, SpConstant.USER_TOKEN);
        token = spUtils.getString(SpConstant.USER_TOKEN);
        mymid = spUtils.getLong(SpConstant.USER_MID) + "";
        BillRecordDetailPresenter billRecordDetailPresenter = new BillRecordDetailPresenter(new BillCall());
        billRecordDetailPresenter.reqeust("Bearer " + token, logId);
        getOtherUserInfoPresenter = new GetOtherUserInfoPresenter(new UserInfoCall());
    }

    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }

    @OnClick(R.id.bt_to_main)
    public void toMain() {
        intent(MainActivity.class);
    }

    class BillCall implements DataCall<BillRecordDetail> {
        @Override
        public void success(BillRecordDetail data, Object... args) {
//            getOtherUserInfoPresenter.reqeust(data.getPrePayId() + "",mymid);
            if (data.getName()!=null&&data.getImgUrl()!=null){
                tv_nickname.setText(data.getName());
                Glide.with(BillRecordDetailActivity.this).load(data.getImgUrl()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(iv_user_head);
            }else{
                getOtherUserInfoPresenter.reqeust(data.getMid() + "",mymid);
            }
            tv_money.setText(data.getMoney() + "");
            tv_status.setText(data.getStatus() == 1 ? "交易成功" : "交易无效");
            tv_pay_cate.setText(data.getTips());
            tv_order_number.setText(data.getTradeNo());
            tv_pay_time.setText(data.getPayTime());
            tv_fee.setText("￥" + data.getFee());
            tv_feeScale.setText(data.getFeeScale()+"%");
            switch (data.getMethod()) {
                case 0:
                    tv_pay_type.setText("余额");
                    break;
                case 1:
                    tv_pay_type.setText("微信");
                    break;
                case 2:
                    tv_pay_type.setText("支付宝");
                    break;
            }
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }

    class UserInfoCall implements DataCall<User> {

        @Override
        public void success(User data, Object... args) {
            tv_nickname.setText(data.getNickName());
            Glide.with(BillRecordDetailActivity.this).load(data.getImgUrl()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(iv_user_head);
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }

    @Override
    protected void destoryData() {

    }
}
